/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.algorithm.jianzhi;

import com.hyts.algorithm.Solution;
import org.junit.Test;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.algorithm.jianzhi
 * @author:LiBo/Alex
 * @create-date:2019-10-27 11:08
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 * 题目描述
 * 请实现一个函数，将一个字符串中的空格替换成“%20”。例如，当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
 * 思路解析：此题用Java来实现是很简单的，一行代码即可str.toString().replaceAll(“\s”, “%20”);，这是考API的情况，
 * 出题者不一定想考这个，还有一种思路是用下边的代码实现的。
 */
public class TestReplaceSpace implements Solution {



    @Test
    @Override
    public void solution1() {
        String testStr = "12345 67 8 9   0";
        char c = ' ';
        final char[] nc = {'%','2','0'};
        List<Integer> pointer = new LinkedList<>();
        char[] teary = testStr.toCharArray();
        int count=0;
        for(int i = 0; i< teary.length;i++){
            if(teary[i] == c){
                count++;
            }
        }
        char[] nteary = new char[teary.length+(count<<1)];
        for(int i = 0,j=i; i< teary.length;i++){
            if(teary[i] == c){
                System.arraycopy(nc,0,nteary,j,3);
                j+=3;
            }else{
                nteary[j] = teary[i];
                j++;
            }
        }
        System.out.println(nteary.length);
        System.out.println(new String(nteary));
    }

    @Override
    public void solution2() {
        String str = "12345 67 8 9   0";
        char[] strToArray = str.toCharArray();
        int count = 0;
        for (int i=0;i<strToArray.length;i++){
            if(strToArray[i] == ' '){
                count++;
            }
        }
        char[] result = new char[strToArray.length + count *2];
        int resIndex = 0;
        for(int i=0;i<strToArray.length;i++){
            if(strToArray[i] == ' '){
                result[resIndex++] = '%';
                result[resIndex++] = '2';
                result[resIndex++] = '0';
            }else{
                result[resIndex++] = strToArray[i];
            }
        }
    }


    @Override
    public void solution3() {


    }


}
